﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules;
using Konk.Modules.TaskManager.Data;


namespace Konk.Modules.TaskManager.Components
{
    public class TaskController
    {

        public static Task GetTask(int taskId)
        {
            //CBO pertence a framework dotnetnuke
            // is a class utility, performs Business Object hydration
            //Hydration is a term that refers to the filling of an instance of a class, in this case from a DataReader
            //CBO exposes both standard and generic versions of two methods; FillObject (which is used to hydrate a single object) 
            //and FillCollection (which is used to hydrate a collection of objects).
            //These methods take the type of the object to hydrate as a parameter and use reflection to determine how to hydrate the object.
           return CBO.FillObject<Task>( DataProvider.Instance().GetTask(taskId));
        }


        public static List<Task> GetTasks(int moduleId)
        {

            return CBO.FillCollection<Task>(DataProvider.Instance().GetTasks(moduleId));
        }


        public static void DeleteTask(int taskId)
        {
            DataProvider.Instance().DeleteTask(taskId);
        }

        public static void deleteTasks(int moduleId)
        {
            DataProvider.Instance().DeteteTasks(moduleId);
        }


        public static int SaveTask(Task t, int tabId)
        {

            if (t.TaskId < 1)
            {
                t.TaskId = DataProvider.Instance().addTask(t);

                //add content item integration
                var cntTaxonomy = new Taxonomy.Content();
                var objContentItem = cntTaxonomy.CreateContentItem(t, tabId);

                //actualizar a task "t" na propriedade ContentItemId
                t.ContentItemId = objContentItem.ContentItemId;

                //guardar a task (faz UPdate)
                //com a chamada recursiva irá executar o "else"
                SaveTask(t, tabId);
            }
            else
            {

                DataProvider.Instance().UpdateTask(t);
                //porque o update pode ser chamado por outro método
                var cntTaxonomy = new Taxonomy.Content();
                cntTaxonomy.UpdateContentItem(t, tabId);

            }

            return t.TaskId;
        }

    }
}